<?php
/**
 * Global configuration
 *
 * PHP version 5
 *
 * @category   Web
 * @package    App
 * @subpackage Files
 * @author     Logger Testenborg <logger.testenborg@data2people.com>
 * @copyright  data2people.com
 * @license    Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt
 * @version    SVN: $Id: $
 *
 */

/**
 * Performs initialization
 *
 * @return void
 *
 */
function initConfig() {
    initZF();
    initZendConfig();
    initSmarty();
    initLogger();
}

/**
 * Setups includes & Zend Framework
 *
 * @return void
 *
 */
function initZF()
{
    /*
     * Set up include dirs
     */
    set_include_path(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'libs'
    .PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'..'
    .PATH_SEPARATOR.dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'libs'.DIRECTORY_SEPARATOR.'smarty'
    .PATH_SEPARATOR.get_include_path());

    require_once 'Zend/Loader.php';
    Zend_Loader::registerAutoload('Zend_Loader');

    /* Init registry */
    require_once 'Zend/Registry.php';

    Zend_Registry::set('ROOT_DIRECTORY', realpath(dirname(__FILE__)
    .DIRECTORY_SEPARATOR.'..'));
    Zend_Registry::set('APP_DIRECTORY', realpath(dirname(__FILE__)
    .DIRECTORY_SEPARATOR));

}

/**
 * Inits Zend_Logger
 *
 * @return void
 */

function initLogger(){
    $logger     = new Zend_Log();
    $fileWriter = new Zend_Log_Writer_Stream(dirname(__FILE__).'/../logs/out.log');
    $logger->addWriter($fileWriter);
    Zend_Registry::set('logger',$logger);
}

/**
 * Inits Smarty templace engine
 *
 */
function initSmarty()
{
    $config       = Zend_Registry::get('config');
    $caching      = (boolean) $config->smarty->caching;
    $debugging    = (boolean) $config->smarty->debugging;
    $fCompile     = (boolean) $config->smarty->force_compile;
    $compileCheck = (boolean) $config->smarty->compile_check;

    Zend_Registry::set('smarty_config', array(
        'caching'        => $caching,
        'cache_lifetime' => $config->smarty->cache_lifetime,
        'template_dir'   => dirname(__FILE__).$config->smarty->template_dir,
        'compile_dir'    => dirname(__FILE__).$config->smarty->compile_dir,
        'config_dir'     => dirname(__FILE__).$config->smarty->config_dir,
        'cache_dir'      => dirname(__FILE__).$config->smarty->cache_dir,
        'debugging'      => $debugging,
        'force_compile'  => $fCompile,
        'compile_check'  => $compileCheck,
        'left_delimiter' => '{',
        'right_delimiter' => '}'
        ));
}

/*
 * Inits Zend_Config
 */
function initZendConfig()
{
    if (!defined("STAGING")) {
        define('STAGING', 'production');
    }
    $config = new Zend_Config_Xml(dirname(__FILE__).DIRECTORY_SEPARATOR
                                 .'Config'. DIRECTORY_SEPARATOR
                                 .'config.xml', STAGING);
    Zend_Registry::set('config', $config);
}
